import time
from selenium import webdriver
from selenium.common.exceptions import InvalidArgumentException, WebDriverException, NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class Base:
    def __init__(self,browser):
        if browser == 'chrome':
            self.driver = webdriver.Chrome()
        elif browser == 'firefox':
            self.driver = webdriver.Firefox
        elif browser == 'Edge':
            self.driver = webdriver.Edge()
        else:
            print('不支持该浏览器')
            self.driver = None
    def get(self,url):
        # 请求网站
        try :
              self.driver.get(url)

        except (InvalidArgumentException, WebDriverException):
            print('网站输入失败')

    def find_element(self,locator,):
        # 定位元素
        try:
            return self.driver.find_element(*locator)

        except NoSuchElementException :
            print('定位失败')
    #  获取属性
    def get_attribute(self,locator,value):
        gg = self.driver.find_element(locator)
        print(gg.get_attribute(value))

    # 浏览器后退
    def back(self):
        self.driver.back()

     # 刷新
    def refresh(self):
        self.driver.refresh()

    # 前进
    def forward(self):
        self.driver.forward()
    # 时间
    def quit(self,seconds=5):
        time.sleep(seconds)
        self.driver.quit()

if __name__ == '__main__':
    base =Base('chrome')
    base.get('https://www.baidu.com')
    base.find_element((By.ID,'kw'))
    base.get_attribute((By.ID,'kw'),'outerHTML')
    base.back()
    base.refresh()
    base.forward()
    base.quit()




